/**
 * Widget Meteo : Projet Android 
 * @author Floriance BRIN, Julie ESCOFFIER, Yoann GOBERT, Ikram NACIRI, Yann NIVET
 * @version 1.0
 * @date mars 2011
 * **/

package fr.imac.ProjetAndroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;

/**
 * Cette activité correspond à aux settings de l'application
 * L'utilisateur peux choisir le thème de l'application.
 * Il peut mettre connecter ou deconnecter le GPS à l'application
 * Attention : il n'est pas possible à partir de l'application de disable le GPS
 * L'utilisateur doit aller dans les settings de son mobile pour ça.
 */
public class SettingsActivity extends Activity {
	private SharedPreferences settings;
	private SharedPreferences.Editor editor;
	public static final String PREFS_NAME = "MyPrefsFile";
	public static final String THEME_1 = "theme1";
	public static final String THEME_2 = "theme2";
	private Boolean isGPS;
	private Boolean isWeather;
	private String theme;
	private CheckBox cbInternet;
	private CheckBox cbWeather;
	private RadioButton rbTheme1;
	private RadioButton rbTheme2;

	
	@Override
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);	
		
		settings = getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();

		cbInternet = (CheckBox) findViewById(R.id.checkBoxInternet);
		cbWeather = (CheckBox) findViewById(R.id.checkBoxWeather);
		rbTheme1 = (RadioButton) findViewById(R.id.theme1);
		rbTheme2 = (RadioButton) findViewById(R.id.theme2);
		
	}
	
	    
	@Override
	public void onStart() {
		super.onStart();
		// On recupere les pref systeme de l'utilisateur
		theme = settings.getString("theme", THEME_1);
		LinearLayout l = (LinearLayout) findViewById(R.id.layoutSettings);
		if(theme==THEME_1){
			rbTheme1.setChecked(true);
			rbTheme2.setChecked(false);
			l.setBackgroundResource(R.drawable.bg);
		} else {
			rbTheme1.setChecked(false);
			rbTheme2.setChecked(true);
			l.setBackgroundResource(R.drawable.bg_);
		}
		
		isGPS = settings.getBoolean("isGPS", true);
		cbInternet.setChecked(isGPS);
		
		isWeather =  settings.getBoolean("isWeather", true);
		cbWeather.setChecked(isWeather);
		
		
		
		
		
		
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		// On envoie les preferences des que l'utilisateur ferme les settings
		editor.putBoolean("isWeather", cbWeather.isChecked());
		editor.putBoolean("isGPS", cbInternet.isChecked());
		if(rbTheme1.isChecked()==true) {
			editor.putString("theme", THEME_1);
		} else editor.putString("theme", THEME_2);
		editor.commit();
		
	}

	@Override
	public void onStop() {
	
		super.onStop();
		editor.putBoolean("isWeather", cbWeather.isChecked());
		editor.putBoolean("isGPS", cbInternet.isChecked());
		if(rbTheme1.isChecked()==true) {
			editor.putString("theme", THEME_1);
		} else editor.putString("theme", THEME_2);
		// Commit the edits!
		editor.commit();
		
	}
	    
	@Override
	public void onDestroy() {
		//Log.i("toto", "onDestroy SETTINGS ");
		
		super.onDestroy();
		
	}
	    
	/**
	* Cr�ation des bo�tes de dialogue
	*/
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		/**case DIALOG_ID:
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle(R.string.import_export);
			dialog.setIcon(android.R.drawable.ic_dialog_info);
			dialog.setItems(R.array.import_export,null);
			dialog.setPositiveButton(android.R.string.ok, null);
			dialog.setNegativeButton(android.R.string.cancel, null);
			dialog.create();

			return dialog;
			break;
		**/
			default:
				return super.onCreateDialog(id);
		}
	}
}
